
from space import *
from image_space import *
import pylab

def plot(element, space = None):
    """plot an element in a given space"""
    assert isinstance(element, Space.Element)
    if space:
        element = space.as_element(element)
    assert isinstance(element.space, DiscreteMappingSpace)
    assert element.space.vtoi.inverse
    
    itov = element.space.vtoi.inverse
    x = [itov(i).coords() for i in range(len(element.coords()))]
    y = element.coords()
    
    pylab.ion()
    pylab.plot(x,y)
    pylab.draw()
    
if __name__ == '__main__':
    print '== plot example =='
    
    image_data = array([1,2,3,5])
    pos_s = real
    func_space = MappingSpace(pos_s, real)
    shape = ((4,0.,1.),)
    image_s = ImageMapping(func_space, shape)
    image = image_s(image_data)  # TODO: avoid the flat
    print "image(0) = ", image(0)
    
    plot(image)
    raw_input()
